package com.dream.keigezhushou.Activity.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.dream.keigezhushou.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *  自定义歌词文本控件
 */

public class MyLrcView extends View {

    private List<KrcLine> mKrcLineList = new ArrayList<KrcLine>();// 存放歌词
    private float mTextSize; //字体
    private int mRows; //歌词行数
    private float mDiverHight; //行间距
    private int normalTextColor; //正常歌词的颜色
    private int currentTextColor; //当前行的颜色
    private int mLrcHeight; //Lrc的高度
    private Paint mNormalPaint; //歌词的字体大小
    private Paint mCurrentPaint; //当前歌词的大小
    private int mViewWidth; //view的宽度

    public MyLrcView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(attrs);
    }

    public MyLrcView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(attrs);
    }


    //初始化操作
    private void initViews(AttributeSet attrs){

        //解析自定义属性
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.Lrc);
        mTextSize = typedArray.getDimension(R.styleable.Lrc_textSize,50.0f);
        mRows = typedArray.getInteger(R.styleable.Lrc_rows,10);
        mDiverHight = typedArray.getDimension(R.styleable.Lrc_dividerHeight,0.0f);
        normalTextColor = typedArray.getColor(R.styleable.Lrc_normalTextColor, Color.GRAY);
        currentTextColor = typedArray.getColor(R.styleable.Lrc_currentTextColor, Color.WHITE);
        typedArray.recycle();

        /**
         *
         *  初始化画板，计算画板的高度
         */
        mLrcHeight = (int)(mTextSize + mDiverHight) * mRows + 5;
        mNormalPaint = new Paint();
        mCurrentPaint = new Paint();

        //初始化画笔
        mNormalPaint.setTextSize(mTextSize);
        mNormalPaint.setColor(normalTextColor);
        mCurrentPaint.setTextSize(mTextSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        //获取view的宽度
        mViewWidth = getMeasuredWidth();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 重新设置view的高度
        int measuredHeight = MeasureSpec.makeMeasureSpec(mLrcHeight, MeasureSpec.AT_MOST);
        setMeasuredDimension(widthMeasureSpec, measuredHeight);
        //setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mKrcLineList.isEmpty()){

            //如果歌词为空，直接返回
            return;
        }

        canvas.save();

    }


    //用来存储歌词的类
    class KrcLineTime{
        long startTime;
        long spanTime;
        long reserve;
        public KrcLineTime(){
        }
        public KrcLineTime(long startTime, long spanTime, long reserve) {
            super();
            this.startTime = startTime;
            this.spanTime = spanTime;
            this.reserve = reserve;
        }

    }
    //用来存储一行歌词的类
    class KrcLine{
        KrcLineTime lineTime;
        List<KrcLineTime> wordTime;
        String lineStr;
        public KrcLine(){
            lineTime = new KrcLineTime();
            wordTime = new ArrayList<KrcLineTime>();
            lineStr="";
        }
    }
}
